<?php

/**
 * 类、模型载入及实例化统一入口文件
 *
 * 管理框架、应用程序所使用的类、模型、函数文件的导入及实例化
 *
 * @author Lan <lanyg.com@gmail.com>
 * @link http://www.lanyg.com/projects/lanp
 * @copyright Copyright &copy; 2011-2012 Yanguang Lan (www.lanyg.com)
 * @license LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @version $Id$
 */

/**
 * MVC_Load
 *
 * @package lanp
 * @author Lan
 */

class MVC_Load
{
    /**
     * 构造函数
     *
     * @access public
     */

    public function  __construct()
    {

    }

    /**
     * 模型载入
     *
     * @access public
     * @param string $model_name 模型名称
     * @param string $pool_name 数据库连接池
     * @return boolean
     */

    public function model($model_name, $pool_name = null)
    {
        if ( empty ($model_name) )
            $model_name = 'MVC_Model';

        if( method_exists($this,$model_name) )
            throw new Exception("Model name '{$model_name}' is an invalid (reserved) name");

        if ( isset ($this->$model_name) )
            return TRUE;

        $controller = MVC::instance(null, 'controller');
        $class_name = $model_name . '_Model';
        $controller->$model_name = new $class_name($pool_name);

        return TRUE;
    }
    
    /**
     * 类载入
     * 
     * @access public
     * @param string $lib_name 类名称
     * @param string $lib_alias 类别名
     * @param string $args 类初始化参数
     */

    public function library($lib_name, $lib_alias = null, $args = null)
    {
        $lib_alias = !empty ($lib_alias) ? $lib_alias : $lib_name;

        if ( empty ($lib_alias) )
            throw new Exception("Library name cannot be empty");

        if ( method_exists($this, $lib_alias) )
            throw new Exception("Library name '{$lib_alias}' is an invalid (reserved) name");

        if ( isset ($this->$lib_alias) )
            return TRUE;

        $controller = MVC::instance(null, 'controller');
        $class_name = 'Library_' . $lib_name;
        $controller->$lib_alias = new $class_name($args);

        return TRUE;
    }

    /**
     * 脚本载入
     *
     * @access public
     * @param string $script_name
     */

    public function script($script_name)
    {
        $filename = strtolower("Script_{$script_name}.php");

        $filepath = APPDIR . 'plugins' . DS . $filename;
        if(!file_exists($filepath))
          $filepath = BASEDIR . 'sharefiles' . DS . 'plugins' . DS . $filename;
        if(!file_exists($filepath))
          $filepath = BASEDIR . 'systemfiles' . DS . 'plugins' . DS . $filename;

        if(!file_exists($filepath))
          throw new Exception("Unknown script file '{$filename}'");

        return require_once($filepath);
    }

    /**
     * 载入数据库接口
     *
     * @access public
     * @param string $pool_name 连接池
     */

    public function database($pool_name = null)
    {
        static $dbs = array();

        include APPDIR . 'configs' . DS . 'database.php';

        if ( empty ($pool_name) )
            $pool_name = 'default';

        if ( $pool_name && isset ($dbs[$pool_name]) )
            return $dbs[$pool_name];

        if ( $pool_name && !empty ($config[$pool_name]['plugin']) ) {
            $dbs[$pool_name] = new $config[$pool_name]['plugin']($config[$pool_name]);
            return $dbs[$pool_name];
        }
    }
}
?>